<template>
  <div class="app-container cam-page">
    <div class="cam-content">
      <section class="panel cam-banner">
        <div class="banner-text">
          <h2>违规管理</h2>
          <p>全程记录违规行为与处理流程，确保校园行车更加规范。</p>
        </div>
        <div class="banner-stat">
          <span class="stat-label">当前记录</span>
          <span class="stat-value">{{ total }}</span>
        </div>
      </section>
      <section class="panel search-panel" v-show="showSearch">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="auto" class="cam-form">
      <el-form-item label="电动车ID" prop="vehicleId">
        <el-input
          v-model="queryParams.vehicleId"
          placeholder="请输入电动车ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人员ID" prop="personId">
        <el-input
          v-model="queryParams.personId"
          placeholder="请输入人员ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处罚措施" prop="penalty">
        <el-input
          v-model="queryParams.penalty"
          placeholder="请输入处罚措施"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="违规时间" prop="violationTime">
        <el-date-picker clearable
          v-model="queryParams.violationTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择违规时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="处理人ID" prop="handlerId">
        <el-input
          v-model="queryParams.handlerId"
          placeholder="请输入处理人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
        </el-form>
      </section>

      <section class="panel toolbar-panel">
        <el-row :gutter="10" class="mb8 cam-toolbar">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:violationofwtd:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:violationofwtd:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:violationofwtd:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:violationofwtd:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </section>

      <section class="panel table-panel">
        <div class="table-shell">
    <el-table v-loading="loading" :data="violationofwtdList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="违规记录ID" align="center" prop="violationId" />
      <el-table-column label="电动车ID" align="center" prop="vehicleId" />
      <el-table-column label="人员ID" align="center" prop="personId" />
      <el-table-column label="违规类型" align="center" prop="violationType" />
      <el-table-column label="证据图片" align="center" width="140">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.evidenceImage"
            :src="scope.row.evidenceImage"
            fit="cover"
            style="width: 64px; height: 64px; border-radius: 8px"
            :preview-src-list="[scope.row.evidenceImage]"
          >
            <div slot="error" class="evidence-empty">无</div>
          </el-image>
          <span v-else class="evidence-empty">无</span>
        </template>
      </el-table-column>
      <el-table-column label="处罚措施" align="center" prop="penalty" />
      <el-table-column label="违规时间" align="center" prop="violationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.violationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center" prop="status" />
      <el-table-column label="处理人ID" align="center" prop="handlerId" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:violationofwtd:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:violationofwtd:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
        </div>
        <div class="pagination-wrap">
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </section>

    </div>

    <!-- 添加或修改违规管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="电动车ID" prop="vehicleId">
          <el-input v-model="form.vehicleId" placeholder="请输入电动车ID" />
        </el-form-item>
        <el-form-item label="人员ID" prop="personId">
          <el-input v-model="form.personId" placeholder="请输入人员ID" />
        </el-form-item>
        <el-form-item label="证据图片" prop="evidenceImage">
          <div class="evidence-upload-area">
            <el-upload
              class="evidence-upload"
              action=""
              :show-file-list="false"
              :before-upload="handleEvidenceBeforeUpload"
            >
              <el-button size="mini" type="primary">选择图片</el-button>
            </el-upload>
            <el-button
              v-if="form.evidenceImage"
              type="text"
              size="mini"
              @click="removeEvidence"
            >移除</el-button>
          </div>
          <el-image
            v-if="form.evidenceImage"
            :src="form.evidenceImage"
            :preview-src-list="[form.evidenceImage]"
            fit="cover"
            class="dialog-evidence-preview"
          >
            <div slot="error" class="evidence-empty">预览失败</div>
          </el-image>
        </el-form-item>
        <el-form-item label="处罚措施" prop="penalty">
          <el-input v-model="form.penalty" placeholder="请输入处罚措施" />
        </el-form-item>
        <el-form-item label="违规时间" prop="violationTime">
          <el-date-picker clearable
            v-model="form.violationTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择违规时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="处理人ID" prop="handlerId">
          <el-input v-model="form.handlerId" placeholder="请输入处理人ID" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listViolationofwtd, getViolationofwtd, delViolationofwtd, addViolationofwtd, updateViolationofwtd } from "@/api/system/violationofwtd"

export default {
  name: "Violationofwtd",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 违规管理表格数据
      violationofwtdList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vehicleId: null,
        personId: null,
        violationType: null,
        penalty: null,
        violationTime: null,
        status: null,
        handlerId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        vehicleId: [
          { required: true, message: "电动车ID不能为空", trigger: "blur" }
        ],
        personId: [
          { required: true, message: "人员ID不能为空", trigger: "blur" }
        ],
        violationType: [
          { required: true, message: "违规类型不能为空", trigger: "change" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询违规管理列表 */
    getList() {
      this.loading = true
      listViolationofwtd(this.queryParams).then(response => {
        this.violationofwtdList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        violationId: null,
        vehicleId: null,
        personId: null,
        violationType: null,
        evidenceImage: null,
        penalty: null,
        violationTime: null,
        status: null,
        handlerId: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.violationId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加违规管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const violationId = row.violationId || this.ids
      getViolationofwtd(violationId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改违规管理"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.violationId != null) {
            updateViolationofwtd(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addViolationofwtd(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    handleEvidenceBeforeUpload(file) {
      const isImage = file.type.startsWith("image/");
      if (!isImage) {
        this.$message.error("请上传图片文件");
        return false;
      }
      const reader = new FileReader();
      reader.onload = e => {
        this.form.evidenceImage = e.target.result;
      };
      reader.readAsDataURL(file);
      return false;
    },
    removeEvidence() {
      this.form.evidenceImage = null;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const violationIds = row.violationId || this.ids
      this.$modal.confirm('是否确认删除违规管理编号为"' + violationIds + '"的数据项？').then(function() {
        return delViolationofwtd(violationIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/violationofwtd/export', {
        ...this.queryParams
      }, `violationofwtd_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style lang="scss" scoped>
.cam-page {
  background: linear-gradient(130deg, #eef2ff 0%, #fbf0ff 45%, #ebf9ff 100%);
  min-height: calc(100vh - 86px);
  padding: 24px;
}

.cam-content {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.panel {
  --panel-bg: linear-gradient(180deg, rgba(255, 255, 255, 0.98), rgba(253, 252, 255, 0.92));
  position: relative;
  background:
    var(--panel-bg) padding-box,
    linear-gradient(120deg, rgba(95, 137, 255, 0.35), rgba(241, 148, 255, 0.32), rgba(189, 228, 255, 0.4)) border-box;
  border-radius: 18px;
  border: 4px solid transparent;
  box-shadow: 0 30px 65px -45px rgba(21, 32, 67, 0.9);
  padding: 20px 24px;
  backdrop-filter: blur(8px);
  z-index: 0;
}

.cam-banner {
  --panel-bg: linear-gradient(120deg, rgba(85, 110, 255, 0.18), rgba(234, 86, 255, 0.14));
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.banner-text h2 {
  margin: 0 0 6px;
  font-size: 22px;
  color: #2b2d42;
  font-weight: 600;
}

.banner-text p {
  margin: 0;
  color: #5b6180;
  font-size: 14px;
}

.banner-stat {
  text-align: right;
  color: #252846;
}

.stat-label {
  font-size: 13px;
  display: block;
  margin-bottom: 4px;
  opacity: 0.8;
}

.stat-value {
  font-size: 34px;
  line-height: 1;
  font-weight: 600;
}

.search-panel {
  padding-bottom: 6px;
}

.cam-form {
  width: 100%;
}

.search-panel ::v-deep .el-form-item {
  margin-right: 12px;
  margin-bottom: 16px;
}

.search-panel ::v-deep .el-input__inner,
.search-panel ::v-deep .el-date-editor .el-input__inner {
  border-radius: 10px;
  background: #f5f7ff;
  border-color: transparent;
  box-shadow: inset 0 0 0 1px rgba(91, 105, 207, 0.08);
}

.search-panel ::v-deep .el-input__inner:focus,
.search-panel ::v-deep .el-date-editor .el-input__inner:focus {
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.35);
}

.toolbar-panel {
  padding: 14px 24px 10px;
}

.cam-toolbar {
  width: 100%;
}

.toolbar-panel ::v-deep .el-row {
  align-items: center;
  flex-wrap: wrap;
  row-gap: 8px;
}

.toolbar-panel ::v-deep .el-col {
  min-width: 120px;
}

.toolbar-panel ::v-deep .el-button {
  border-radius: 999px;
  padding: 0 18px;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.toolbar-panel ::v-deep .el-button:hover {
  transform: translateY(-1px);
}

.toolbar-panel ::v-deep .el-button--primary {
  box-shadow: 0 15px 35px -20px rgba(64, 158, 255, 1);
}

.toolbar-panel ::v-deep .right-toolbar {
  margin-left: auto;
  padding-top: 4px;
}

.table-panel {
  padding: 18px 24px 8px;
}

.table-shell {
  border-radius: 18px;
  overflow: hidden;
  border: 4px solid transparent;
  background:
    linear-gradient(180deg, #ffffff, #fdfdff) padding-box,
    linear-gradient(120deg, rgba(93, 144, 255, 0.35), rgba(179, 231, 255, 0.4)) border-box;
  box-shadow: 0 35px 65px -45px rgba(0, 16, 54, 0.9);
}

.table-panel ::v-deep .el-table {
  background-color: white;
}

.table-panel ::v-deep .el-table__header-wrapper th {
  background: linear-gradient(90deg, #f9faff 0%, #f0f4ff 100%);
  color: #475467;
  font-weight: 600;
}

.table-panel ::v-deep .el-table__body tr {
  transition: background 0.2s ease;
}

.table-panel ::v-deep .el-table__body tr:hover > td {
  background: rgba(98, 117, 255, 0.08);
}

.table-panel ::v-deep .el-table .cell {
  color: #2f3142;
}

.pagination-wrap {
  display: flex;
  justify-content: flex-end;
  padding: 16px 4px 0;
}

.evidence-upload-area {
  display: flex;
  align-items: center;
  gap: 8px;
}

.dialog-evidence-preview {
  margin-top: 8px;
  width: 160px;
  height: 110px;
  border-radius: 10px;
  box-shadow: 0 6px 18px -10px rgba(32, 45, 76, 0.5);
}

.evidence-empty {
  color: #999;
  font-size: 12px;
}

@media (max-width: 1180px) {
  .cam-content {
    max-width: 100%;
  }

  .banner-stat {
    text-align: left;
  }
}

@media (max-width: 768px) {
  .cam-page {
    padding: 16px;
  }

  .cam-banner {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .toolbar-panel ::v-deep .el-col {
    min-width: 140px;
  }
}
</style>
